/**
 * create by caijy 2019/11/30
 */
define(["echarts"], function (echarts) {

    var self,
        chartDiv,
        selfData = {
            list: [],
            names: [],
            percents: [],
            startDate: '',
            endDate: '',
            div1: true,
            div2: true,
            div3: true,
            div4: true,
        },
        typeData = {},
        showData={
            formData:[],
            chartData:{},
            headArray:[]
        }
    ;

    app.newModule({

        init: function () {
            self = this;

            self._super({
                _readyBefore: function () {

                }
            }, win);

        },
        getInfo: function () {

            app.ajax({
                xurl: 'api/wx/xsfx/getInfo',
                loading: true,
                xdata: {
                    startDate: selfData.startDate,
                    endDate: selfData.endDate
                },
                cb: function (res) {
                    if (res.ok) {
                        var data = res.data.data;
                        for (var i = 0, j = data.length; i < j; i++) {
                            var item = data[i];
                            item['percent'] = (item.TARGETAMOUNT == 0 ? 100 : item.AMOUNT.div(item.TARGETAMOUNT) * 100).toFixed(2);
                            item['percent'] = Math.round(item['percent']*100)/100;
                            if (showData.headArray.length > 0) {
                                switch (showData.headArray[0]) {
                                    case 0:
                                        item.showName = item.GWCS014NAME;
                                        // break;
                                    case 1:
                                        item.showName = item.showName +'|'+item.GWCS024NAME;
                                        // break;
                                    case 2:
                                        item.showName = item.showName +'|'+item.GWAM004NAME;
                                        // break;
                                    case 3:
                                        item.showName = item.showName +'|'+item.GWAK054NAME;
                                        // break;
                                }
                            }else {
                                item.showName=item['GWCS014NAME']+'|'+item.GWCS024NAME+'|'+item.GWAM004NAME+'|'+item.GWAK054NAME;
                            }
                            item.compareCode = item.showName;

                        }

                        data.sortByKey('AMOUNT', true);
                        typeData['t1234'] = data;
                        selfData.list.pushArray(typeData['t1234'], true);
                        showData.formData.pushArray(data,true);
                        self.formChat(showData.formData)
                    }
                }
            })
        },
        formChat: function (data) {

            var pieData = [];

            for (var i = 0, j = data.length; i < j; i++) {
                var item = data[i];
                pieData.push({
                    value: item.AMOUNT,
                    name: item.compareCode.substr(0,item.compareCode.length-1)
                    // name: item.showName
                });
            }

            var option = {
                backgroundColor: '#fff',
                grid: {
                    top: 10,
                    left: 10,
                    right:10,
                    bottom: 30,
                    containLabel: true//这常用于『防止标签溢出』的场景
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                calculable: true,
                series: [{
                    name: '访问来源',
                    type: 'pie',
                    radius: '65%',
                    center: ['50%', '50%'],
                    data: pieData,
                    label: {
                        normal: {
                            formatter: function (a) {
                                return a.data.name + "(" + a.data.value + ")";
                            },
                            show: true,
                        },
                        emphasis: {
                            show: true,
                            textStyle: {
                                fontSize: '15',
                                fontWeight: 'bold'
                            }
                        }
                    },
                }]
            };

            chartDiv.setOption(option);
        },
        getShowData:function(){
            var data = app.cloneJSON(typeData['t1234']);
            var showArray=[];
            data.forEach(function (d, index) {
                var show = {};//show的参数不固定
                //获取checked=true代表的取值
                var compareCode = '';
                var firstName = '';
                showData.headArray.forEach(function (headIndex, index) {
                    switch (headIndex) {
                        case 0:
                            show.GWCS014NAME = d.GWCS014NAME;
                            compareCode +=  d.GWCS014NAME+'|';
                            break;
                        case 1:
                            show.GWCS024NAME = d.GWCS024NAME;
                            compareCode +=  d.GWCS024NAME+'|';
                            break;
                        case 2:
                            show.GWAM004NAME = d.GWAM004NAME;
                            compareCode +=  d.GWAM004NAME+'|';
                            break;
                        case 3:
                            show.GWAK054NAME = d.GWAK054NAME;
                            // compareCode +=  d.GWAK054NAME+'|';
                            if(d.GWAK054NAME == undefined || d.GWAK054NAME =='' || d.GWAK054NAME.length < 1){
                            }else {
                                compareCode +=  d.GWAK054NAME+'|';
                            }
                            break;
                    }
                    if (index == 0)
                        firstName = compareCode.replace('|','')
                });
                data.iFilter({
                    o: show,
                    cb: function (arr, i) {
                        if (i > 0){
                            show.TARGETAMOUNT=0;
                            show.AMOUNT=0;
                            arr.forEach(function (a, index) {
                                show.TARGETAMOUNT += a.TARGETAMOUNT;
                                show.AMOUNT += a.AMOUNT;
                            })
                            //计算：总实际除以总目标
                            var percent =(show.TARGETAMOUNT == 0 ? 100 : show.AMOUNT.div(show.TARGETAMOUNT) * 100).toFixed(2);
                            show.percent = Math.round(percent*100)/100;
                            show.compareCode = compareCode;
                            show.showName=firstName;
                            var add=true;
                            for (var i = 0; i < showArray.length; i++) {
                                if (showArray[i].compareCode == show.compareCode){
                                    add=false;
                                    break;
                                }
                            }

                            if (add){
                                showArray.push(show);
                            }

                        }
                    }
                });
            });
            selfData.list=showArray;
            self.formChat(selfData.list)
        },
        _ready: function () {

            var monthStartEnd = new Date().getCurrentMonth();
            selfData.startDate = monthStartEnd[0];//本月开始日期
            selfData.endDate = new Date().Format('yyyy-MM-dd');

            self.getInfo();

            new Vue({
                el: doc.querySelector('.mui-content'),
                data: selfData,
                watch: {
                    'startDate': function (res) {
                        selfData.startDate = res;
                        self.getInfo();
                    },
                    'endDate': function (res) {
                        selfData.endDate = res;
                        self.getInfo();
                    }
                },
                methods: {
                    hideDiv: function (type, ele) {
                        var isok=false;
                        var array = [];
                        doc.querySelectorAll('.mui-checkbox input[type=checkbox]').forEach(function (item, index) {
                            if (item.checked){
                                isok=true;
                                array.push(index)
                            }
                        });
                        showData.headArray.pushArray(array,true);
                        var check = ele.target.checked ? true : false;
                        if (!isok){
                            app.toast('至少选择一项!');
                            ele.target.checked=true;
                            return;
                        }
                        self.getShowData();
                        switch (type) {
                            case 1:
                                selfData.div1 = check;
                                break;
                            case 2:
                                selfData.div2 = check;
                                break;
                            case 3:
                                selfData.div3 = check;
                                break;
                            case 4:
                                selfData.div4 = check;
                                break;
                        }
                    }
                }
            });

            chartDiv = echarts.init(doc.getElementById('divChart'));

        },
        _lazy: function () {
            this._super();

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        }
    });

});

